﻿using LibrarySystem.Model;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;

namespace LibrarySystem.ViewModel
{
    public class LoginViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler? PropertyChanged;

        private LoginModel? LoginModel { get; set; } = new LoginModel();

        public string? UserName
        {
            get => LoginModel?.UserName;
            set
            {
                if (LoginModel == null) LoginModel = new LoginModel();
                LoginModel.UserName = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("UserName"));
            }
        }

        public string? Password
        {
            get => LoginModel?.Password;
            set
            {
                if (LoginModel == null) LoginModel = new LoginModel();
                LoginModel.Password = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Password"));
            }
        }

        public void Login()
        {
            if (UserName == "admin" && Password == "123456")
                MessageBox.Show("登录成功。");
            else
            {
                MessageBox.Show("登录失败。");
                UserName = "";
                Password = "";
            }
        }

        public ICommand LoginCommand
        {
            get
            {
                return new RelayCommand(Login, () => true);
            }
        }
    }
}
